/*
 * main.c
 *
 *  Created on: 27/08/2012
 *      Author: marciocs5
 */

#include "BitLinked.h"
#include "BitTools.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <string>
#include <assert.h>

void generate(int prob, BitVector<int>* a){
	for(int i = 0; i < a->size(); i++){
		 if(rand()%101 <= prob)a->toSet(i);
	}
}

int main(int argc, char *argv[]){

	srand(time(NULL));
	FILE* out = fopen("vector.txt","a+");
	int n1 = atoi(argv[1]);
	int n2 = atoi(argv[2]);
	int i = atoi(argv[3]);
	BitVector<int>* bit1 = new BitVector<int>(n1);
	BitVector<int>* bit2 = new BitVector<int>(n1);
	BitVector<int>* bit3 = new BitVector<int>(n1);

	struct timespec start, end;
	double elapsed ,elapsed1, elapsed2, elapsed3;
    struct timeval tv;

    gettimeofday(&tv, NULL);
    start.tv_sec = tv.tv_sec;
    start.tv_nsec = tv.tv_usec * 1000;
	generate(i, bit1);
	generate(i, bit2);
	generate(i, bit3);
	end.tv_sec = tv.tv_sec;
	end.tv_nsec = tv.tv_usec * 1000;
	elapsed = (end.tv_sec - start.tv_sec)*1000;
	elapsed += (end.tv_nsec - start.tv_nsec) / 1000000.0;

    gettimeofday(&tv, NULL);
    start.tv_sec = tv.tv_sec;
    start.tv_nsec = tv.tv_usec * 1000;
    bit1->cardOfset();
    gettimeofday(&tv, NULL);
    end.tv_sec = tv.tv_sec;
    end.tv_nsec = tv.tv_usec * 1000;
    elapsed1 = (end.tv_sec - start.tv_sec)*1000;
    elapsed1 += (end.tv_nsec - start.tv_nsec) / 1000000.0;

    gettimeofday(&tv, NULL);
    start.tv_sec = tv.tv_sec;
    start.tv_nsec = tv.tv_usec * 1000;
    bit2->logicor(bit1);
    gettimeofday(&tv, NULL);
    end.tv_sec = tv.tv_sec;
    end.tv_nsec = tv.tv_usec * 1000;
    elapsed2 = (end.tv_sec - start.tv_sec)*1000;
    elapsed2 += (end.tv_nsec - start.tv_nsec) / 1000000.0;

    gettimeofday(&tv, NULL);
    start.tv_sec = tv.tv_sec;
    start.tv_nsec = tv.tv_usec * 1000;
    bit3->logicand(bit1);
    gettimeofday(&tv, NULL);
    end.tv_sec = tv.tv_sec;
    end.tv_nsec = tv.tv_usec * 1000;
    elapsed3 = (end.tv_sec - start.tv_sec)*1000;
    elapsed3 += (end.tv_nsec - start.tv_nsec) / 1000000.0;

    printf("tempo para gerar = %f, count = %f , and = %f, or = %f \n", elapsed, elapsed1, elapsed2, elapsed3);
    fprintf(out," %d \t %d \t %f \t %f \t %f \t %f \n", n1, i, elapsed, elapsed1, elapsed2, elapsed3);

	return 0;
}

